package com.skyline.user.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.user.dao.AccountInfoDao;
import com.skyline.user.mapper.AccountInfoMapper;
import com.skyline.user.model.User;

//TODO:加注释
@Repository("accountInfoDao")
public class AccountInfoDaoImpl extends BaseDaoImpl implements AccountInfoDao {
	/**
	 * update user set
	 * authority=?,securityQuestion=?,securityAnswer=?,securityEmail=? where
	 * userid=?
	 * */
	@Value("${AccountInfoDao.updateSecurityInfo}")
	private String updateSecurityInfoSql;

	/**
	 * update user set password=? where userid=? and password=?
	 * */
	@Value("${AccountInfoDao.updatePassword}")
	private String updatePasswordSql;

	/**
	 * select userid,authority,securityQuestion,securityAnswer,securityEmail
	 * from user where userid=?
	 * */
	@Value("${AccountInfoDao.queryAccountInfoByUserID}")
	private String queryAccountInfoByUserIDSql;

	/**
	 * select userid,authority,securityQuestion,securityAnswer,securityEmail
	 * from user where email=?
	 * */
	@Value("${AccountInfoDao.queryAccountInfoByEmail}")
	private String queryAccountInfoByEmailSql;

	/**
	 * select userid,securityQuestion from user where email=?
	 * */
	@Value("${AccountInfoDao.querySecurityQuestion}")
	private String querySecurityQuestionSql;

	
	/**
	 * select userid from user where userid=? and securityAnswer=?
	 * */
	@Value("${AccountInfoDao.existSecurityAnswer}")
	private String existSecurityAnswerSql;
	
	@Value("${AccountInfoDao.queryAccountInfoByEmailAndPswd}")
private String queryAccountInfoByEmailAndPswdSql;
	@Override
	public void updateSecurityInfo(Long userid, Integer authority, String securityQuestion, String securityAnswer,
			String securityEMail) {
		jdbcTemplate.update(updateSecurityInfoSql, new Object[] { authority, securityQuestion, securityAnswer,
				securityEMail, userid });
	}

	@Override
	public void updatePassword(Long userid, String newPassword) {
		jdbcTemplate.update(updatePasswordSql, new Object[] { newPassword,userid});
	}

	@Override
	public User queryAccountInfoByUserID(Long userid) {
		List<User> users = jdbcTemplate.query(queryAccountInfoByUserIDSql, AccountInfoMapper.getMapper(), userid);
		if (users.size() > 0) {
			return users.get(0);
		} else {
			return null;
		}
	}

	@Override
	public User queryAccountInfoByEmail(String email) {
		List<User> users = jdbcTemplate.query(queryAccountInfoByEmailSql, AccountInfoMapper.getMapper(), email);
		if (users.size() > 0) {
			return users.get(0);
		} else {
			return null;
		}
	}

	@Override
	public User querySecurityQuestion(String email) {
		List<User> users = jdbcTemplate.query(querySecurityQuestionSql, AccountInfoMapper.getMapper(), email);
		if (users.size() > 0) {
			return users.get(0);
		} else {
			return null;
		}
	}

	@Override
	public boolean existSecurityAnswer(Long userid, String securityAnswer) {
		int count = getCountResult(existSecurityAnswerSql, userid, securityAnswer);
		if (count == 0) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public boolean existSecurityEmail(String token) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public User queryAccountInfoByEmailAndPswd(String email, String password) {
		List<User> users = jdbcTemplate.query(queryAccountInfoByEmailAndPswdSql, AccountInfoMapper.getMapper(), email,password);
		if (users.size() > 0) {
			return users.get(0);
		} else {
			return null;
		}
	}

}
